package com.audaque.jadmin.common.utils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description :
 * @FileName: WebUtils.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年10月19日 下午2:50:16
 * @Version:V1.00
 */
public class WebUtils {

	private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();
	private static final String BASE_PATH_KEY = "basepath";

	public static void setBasePath(String basePath) {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<String, Object>();
			threadLocal.set(map);
		}
		map.put(BASE_PATH_KEY, basePath);
	}

	public static String getBasePath() {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			return null;
		}
		Object o = map.get(BASE_PATH_KEY);
		return o == null ? null : o.toString();
	}

	/**
	 * 是否是Ajax请求
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isAjaxRequest(HttpServletRequest request) {
		boolean ajax = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
		String ajaxFlag = null == request.getParameter("ajax") ? "false" : request.getParameter("ajax");
		boolean isAjax = ajax || ajaxFlag.equalsIgnoreCase("true");
		return isAjax;
	}
}
